# Avoid multiple calls to find_package to append duplicated properties to the targets
include_guard()########### VARIABLES #######################################################################
#############################################################################################
set(librdkafka_FRAMEWORKS_FOUND_RELEASE "") # Will be filled later
conan_find_apple_frameworks(librdkafka_FRAMEWORKS_FOUND_RELEASE "${librdkafka_FRAMEWORKS_RELEASE}" "${librdkafka_FRAMEWORK_DIRS_RELEASE}")

set(librdkafka_LIBRARIES_TARGETS "") # Will be filled later


######## Create an interface target to contain all the dependencies (frameworks, system and conan deps)
if(NOT TARGET librdkafka_DEPS_TARGET)
    add_library(librdkafka_DEPS_TARGET INTERFACE IMPORTED)
endif()

set_property(TARGET librdkafka_DEPS_TARGET
             PROPERTY INTERFACE_LINK_LIBRARIES
             $<$<CONFIG:Release>:${librdkafka_FRAMEWORKS_FOUND_RELEASE}>
             $<$<CONFIG:Release>:${librdkafka_SYSTEM_LIBS_RELEASE}>
             $<$<CONFIG:Release>:LZ4::lz4_static;zstd::libzstd_static;openssl::openssl;cyrus-sasl::cyrus-sasl;RdKafka::rdkafka>
             APPEND)

####### Find the libraries declared in cpp_info.libs, create an IMPORTED target for each one and link the
####### librdkafka_DEPS_TARGET to all of them
conan_package_library_targets("${librdkafka_LIBS_RELEASE}"    # libraries
                              "${librdkafka_LIB_DIRS_RELEASE}" # package_libdir
                              librdkafka_DEPS_TARGET
                              librdkafka_LIBRARIES_TARGETS  # out_libraries_targets
                              "_RELEASE"
                              "librdkafka")    # package_name

# FIXME: What is the result of this for multi-config? All configs adding themselves to path?
set(CMAKE_MODULE_PATH ${librdkafka_BUILD_DIRS_RELEASE} ${CMAKE_MODULE_PATH})

########## COMPONENTS TARGET PROPERTIES Release ########################################

    ########## COMPONENT RdKafka::rdkafka++ #############

        set(librdkafka_RdKafka_rdkafka++_FRAMEWORKS_FOUND_RELEASE "")
        conan_find_apple_frameworks(librdkafka_RdKafka_rdkafka++_FRAMEWORKS_FOUND_RELEASE "${librdkafka_RdKafka_rdkafka++_FRAMEWORKS_RELEASE}" "${librdkafka_RdKafka_rdkafka++_FRAMEWORK_DIRS_RELEASE}")

        set(librdkafka_RdKafka_rdkafka++_LIBRARIES_TARGETS "")

        ######## Create an interface target to contain all the dependencies (frameworks, system and conan deps)
        if(NOT TARGET librdkafka_RdKafka_rdkafka++_DEPS_TARGET)
            add_library(librdkafka_RdKafka_rdkafka++_DEPS_TARGET INTERFACE IMPORTED)
        endif()

        set_property(TARGET librdkafka_RdKafka_rdkafka++_DEPS_TARGET
                     PROPERTY INTERFACE_LINK_LIBRARIES
                     $<$<CONFIG:Release>:${librdkafka_RdKafka_rdkafka++_FRAMEWORKS_FOUND_RELEASE}>
                     $<$<CONFIG:Release>:${librdkafka_RdKafka_rdkafka++_SYSTEM_LIBS_RELEASE}>
                     $<$<CONFIG:Release>:${librdkafka_RdKafka_rdkafka++_DEPENDENCIES_RELEASE}>
                     APPEND)

        ####### Find the libraries declared in cpp_info.component["xxx"].libs,
        ####### create an IMPORTED target for each one and link the 'librdkafka_RdKafka_rdkafka++_DEPS_TARGET' to all of them
        conan_package_library_targets("${librdkafka_RdKafka_rdkafka++_LIBS_RELEASE}"
                                      "${librdkafka_RdKafka_rdkafka++_LIB_DIRS_RELEASE}"
                                      librdkafka_RdKafka_rdkafka++_DEPS_TARGET
                                      librdkafka_RdKafka_rdkafka++_LIBRARIES_TARGETS
                                      "_RELEASE"
                                      "librdkafka_RdKafka_rdkafka++")

        ########## TARGET PROPERTIES #####################################
        set_property(TARGET RdKafka::rdkafka++
                     PROPERTY INTERFACE_LINK_LIBRARIES
                     $<$<CONFIG:Release>:${librdkafka_RdKafka_rdkafka++_OBJECTS_RELEASE}>
                     $<$<CONFIG:Release>:${librdkafka_RdKafka_rdkafka++_LIBRARIES_TARGETS}>
                     APPEND)

        if("${librdkafka_RdKafka_rdkafka++_LIBS_RELEASE}" STREQUAL "")
            # If the component is not declaring any "cpp_info.components['foo'].libs" the system, frameworks etc are not
            # linked to the imported targets and we need to do it to the global target
            set_property(TARGET RdKafka::rdkafka++
                         PROPERTY INTERFACE_LINK_LIBRARIES
                         librdkafka_RdKafka_rdkafka++_DEPS_TARGET
                         APPEND)
        endif()

        set_property(TARGET RdKafka::rdkafka++ PROPERTY INTERFACE_LINK_OPTIONS
                     $<$<CONFIG:Release>:${librdkafka_RdKafka_rdkafka++_LINKER_FLAGS_RELEASE}> APPEND)
        set_property(TARGET RdKafka::rdkafka++ PROPERTY INTERFACE_INCLUDE_DIRECTORIES
                     $<$<CONFIG:Release>:${librdkafka_RdKafka_rdkafka++_INCLUDE_DIRS_RELEASE}> APPEND)
        set_property(TARGET RdKafka::rdkafka++ PROPERTY INTERFACE_COMPILE_DEFINITIONS
                     $<$<CONFIG:Release>:${librdkafka_RdKafka_rdkafka++_COMPILE_DEFINITIONS_RELEASE}> APPEND)
        set_property(TARGET RdKafka::rdkafka++ PROPERTY INTERFACE_COMPILE_OPTIONS
                     $<$<CONFIG:Release>:${librdkafka_RdKafka_rdkafka++_COMPILE_OPTIONS_RELEASE}> APPEND)

    ########## COMPONENT RdKafka::rdkafka #############

        set(librdkafka_RdKafka_rdkafka_FRAMEWORKS_FOUND_RELEASE "")
        conan_find_apple_frameworks(librdkafka_RdKafka_rdkafka_FRAMEWORKS_FOUND_RELEASE "${librdkafka_RdKafka_rdkafka_FRAMEWORKS_RELEASE}" "${librdkafka_RdKafka_rdkafka_FRAMEWORK_DIRS_RELEASE}")

        set(librdkafka_RdKafka_rdkafka_LIBRARIES_TARGETS "")

        ######## Create an interface target to contain all the dependencies (frameworks, system and conan deps)
        if(NOT TARGET librdkafka_RdKafka_rdkafka_DEPS_TARGET)
            add_library(librdkafka_RdKafka_rdkafka_DEPS_TARGET INTERFACE IMPORTED)
        endif()

        set_property(TARGET librdkafka_RdKafka_rdkafka_DEPS_TARGET
                     PROPERTY INTERFACE_LINK_LIBRARIES
                     $<$<CONFIG:Release>:${librdkafka_RdKafka_rdkafka_FRAMEWORKS_FOUND_RELEASE}>
                     $<$<CONFIG:Release>:${librdkafka_RdKafka_rdkafka_SYSTEM_LIBS_RELEASE}>
                     $<$<CONFIG:Release>:${librdkafka_RdKafka_rdkafka_DEPENDENCIES_RELEASE}>
                     APPEND)

        ####### Find the libraries declared in cpp_info.component["xxx"].libs,
        ####### create an IMPORTED target for each one and link the 'librdkafka_RdKafka_rdkafka_DEPS_TARGET' to all of them
        conan_package_library_targets("${librdkafka_RdKafka_rdkafka_LIBS_RELEASE}"
                                      "${librdkafka_RdKafka_rdkafka_LIB_DIRS_RELEASE}"
                                      librdkafka_RdKafka_rdkafka_DEPS_TARGET
                                      librdkafka_RdKafka_rdkafka_LIBRARIES_TARGETS
                                      "_RELEASE"
                                      "librdkafka_RdKafka_rdkafka")

        ########## TARGET PROPERTIES #####################################
        set_property(TARGET RdKafka::rdkafka
                     PROPERTY INTERFACE_LINK_LIBRARIES
                     $<$<CONFIG:Release>:${librdkafka_RdKafka_rdkafka_OBJECTS_RELEASE}>
                     $<$<CONFIG:Release>:${librdkafka_RdKafka_rdkafka_LIBRARIES_TARGETS}>
                     APPEND)

        if("${librdkafka_RdKafka_rdkafka_LIBS_RELEASE}" STREQUAL "")
            # If the component is not declaring any "cpp_info.components['foo'].libs" the system, frameworks etc are not
            # linked to the imported targets and we need to do it to the global target
            set_property(TARGET RdKafka::rdkafka
                         PROPERTY INTERFACE_LINK_LIBRARIES
                         librdkafka_RdKafka_rdkafka_DEPS_TARGET
                         APPEND)
        endif()

        set_property(TARGET RdKafka::rdkafka PROPERTY INTERFACE_LINK_OPTIONS
                     $<$<CONFIG:Release>:${librdkafka_RdKafka_rdkafka_LINKER_FLAGS_RELEASE}> APPEND)
        set_property(TARGET RdKafka::rdkafka PROPERTY INTERFACE_INCLUDE_DIRECTORIES
                     $<$<CONFIG:Release>:${librdkafka_RdKafka_rdkafka_INCLUDE_DIRS_RELEASE}> APPEND)
        set_property(TARGET RdKafka::rdkafka PROPERTY INTERFACE_COMPILE_DEFINITIONS
                     $<$<CONFIG:Release>:${librdkafka_RdKafka_rdkafka_COMPILE_DEFINITIONS_RELEASE}> APPEND)
        set_property(TARGET RdKafka::rdkafka PROPERTY INTERFACE_COMPILE_OPTIONS
                     $<$<CONFIG:Release>:${librdkafka_RdKafka_rdkafka_COMPILE_OPTIONS_RELEASE}> APPEND)

    ########## AGGREGATED GLOBAL TARGET WITH THE COMPONENTS #####################
    set_property(TARGET RdKafka::rdkafka++ PROPERTY INTERFACE_LINK_LIBRARIES RdKafka::rdkafka++ APPEND)
    set_property(TARGET RdKafka::rdkafka++ PROPERTY INTERFACE_LINK_LIBRARIES RdKafka::rdkafka APPEND)

########## For the modules (FindXXX)
set(librdkafka_LIBRARIES_RELEASE RdKafka::rdkafka++)
